/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import device from '@system.device';

const HEAD_IMAGE = ["/common/head0.png", "/common/head1.png", "/common/head2.png", "/common/head3.png", "/common/head4.png"];

export default {
    //数据字段定义
    data: {
        contactList: [
            {
                imageSrc: HEAD_IMAGE[0],
                name: "Alan",
                phone: "020-00000000"
            },
            {
                imageSrc: HEAD_IMAGE[2],
                name: "Bob",
                phone: "020-11111111"
            },
            {
                imageSrc: HEAD_IMAGE[3],
                name: "Carl",
                phone: "0755-33333333"
            },
            {
                imageSrc: HEAD_IMAGE[4],
                name: "David",
                phone: "19876543210"
            },
            {
                imageSrc: HEAD_IMAGE[0],
                name: "Alan",
                phone: "020-00000000"
            },
            {
                imageSrc: HEAD_IMAGE[1],
                name: "Bob",
                phone: "020-11111111"
            },
            {
                imageSrc: HEAD_IMAGE[2],
                name: "Carl",
                phone: "12345678910"
            },
            {
                imageSrc: HEAD_IMAGE[3],
                name: "David",
                phone: "19876543210"
            },
            {
                imageSrc: HEAD_IMAGE[4],
                name: "Alan",
                phone: "020-00000000"
            },
            {
                imageSrc: HEAD_IMAGE[0],
                name: "Bob",
                phone: "020-11111111"
            },
            {
                imageSrc: HEAD_IMAGE[1],
                name: "Carl",
                phone: "12345678910"
            },
            {
                imageSrc: HEAD_IMAGE[2],
                name: "David",
                phone: "19876543210"
            },
            {
                imageSrc: HEAD_IMAGE[3],
                name: "Alan",
                phone: "020-00000000"
            },
            {
                imageSrc: HEAD_IMAGE[4],
                name: "Bob",
                phone: "020-11111111"
            },
            {
                imageSrc: HEAD_IMAGE[0],
                name: "Carl",
                phone: "12345678910"
            },
            {
                imageSrc: HEAD_IMAGE[1],
                name: "David",
                phone: "19876543210"
            }
        ],
        name: "Alan",
        phone: "020-00000000",
        imageSrc: HEAD_IMAGE[0],
        index: 0
    },
    onShow() {
        // 查询当前设备类型
        device.getInfo({
            success: (data) => {
                if (data.deviceType && (data.deviceType === 'tv' || data.deviceType === 'wearable'
                || data.deviceType === 'tablet')) {
                    this.isPhoneDevice = false;
                }
            },
            fail: function (data, code) {
                console.info('Failed to obtain device information. Error code:' + code + '; Error information: ' + data);
                this.isPhoneDevice = false;
            }
        });
    },
    onReady() {
    },

    onInit() {
    },
    clickItem(idx) {
        this.imageSrc = this.contactList[idx].imageSrc;
        this.name = this.contactList[idx].name;
        this.phone = this.contactList[idx].phone;
        this.showDialog();
        this.index = idx;
    },
    showDialog() {
        this.$element('detailDialog').show();
    },
    listFocus(idx) {
        this.imageSrc = this.contactList[idx].imageSrc;
        this.name = this.contactList[idx].name;
        this.phone = this.contactList[idx].phone;
    },
    confirm() {
        //修改对应行后保存到列表中
        this.contactList[this.index].name = this.name;
        this.contactList[this.index].phone = this.phone;
        this.$element('detailDialog').close();
    },
    cancel() {
        this.$element('detailDialog').close();
    },
    // 更新input Name值
    changeName(e) {
        let changeValue = e.text
        this.name = changeValue;
    },
    // 更新input Phone值
    changePhone(e) {
        let changeValue = e.text;
        this.phone = changeValue;
    },
    dialogCancel() {
        this.$element('detailDialog').close();
    }
};